#include <iostream>
#include <cmath>
#include <limits>
#include "EquationSolver.h"

class MyFunction : public Function {
public:
    double operator()(double x) {
        return 5 * acos(-1) - 10 * asin(x) - 10 * x * sqrt(1 - x * x) - 12.4;
    }

    double derivative(double x) {
        static double epsilon = 1e-8;
        return (this->operator()(x + epsilon) - this->operator()(x - epsilon)) / (2 * epsilon);
    }
};

int main() {
    double epsi = std::numeric_limits<double>::epsilon();
    MyFunction f_1;
    Bisection<MyFunction> Bis(0, 1, 100, epsi, f_1);
    Newton<MyFunction> New(0.5, 10, f_1);
    Secant<MyFunction> Sec(0.5, 0.6, 20, epsi, f_1);

    double* R_1 = Bis.solve();
    double* R_2 = New.solve();
    double* R_3 = Sec.solve();

    std::cout << "二分法求解结果：" << std::endl;
    std::cout << "根为" << R_1[0] << std::endl;
    std::cout << "迭代次数为" << R_1[1] << std::endl;
    std::cout << "误差为" << f_1(R_1[0]) << std::endl;
    std::cout << "水的深度为" << 1 - R_1[0] << "ft" << std::endl;
    std::cout <<"*******************************************************"<<std::endl;
    std::cout << "牛顿法求解结果：" << std::endl;
    std::cout << "根为" << R_2[0] << std::endl;
    std::cout << "迭代次数为" << R_2[1] << std::endl;
    std::cout << "误差为" << f_1(R_2[0]) << std::endl;
    std::cout << "水的深度为" << 1 - R_2[0] << "ft" << std::endl;
    std::cout <<"*******************************************************"<<std::endl;    
    std::cout << "割线法求解结果：" << std::endl;
    std::cout << "根为" << R_3[0] << std::endl;
    std::cout << "迭代次数为" << R_3[1] << std::endl;
    std::cout << "误差为" << f_1(R_3[0]) << std::endl;
    std::cout << "水的深度为" << 1 - R_3[0] << "ft" << std::endl;

    // 释放动态分配的内存
    delete[] R_1;
    delete[] R_2;
    delete[] R_3;

    return 0;
}
